//添加编制车辆
var setHtml=`<div class="layui-form-item">
<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="设备名称" autocomplete="off" class="layui-input setname" readonly>
        </div>
        <div class="layui-form-mid">-</div>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="用途" autocomplete="off" class="layui-input setuse" readonly>
        </div>
    </div>
</div>
</div> `;
var id ='{$Think.get.id}';
var set=null;
var statenum=null;
//车辆详情-layui渲染
var data = { //数据
    "title":"Layui常用模块"
    ,"list":[{"modname":"弹层","alias":"layer","site":"layer.layui.com"},{"modname":"表单","alias":"form"}]
  }
  var getTpl = demo.innerHTML
  laytpl(getTpl).render(data, function(html){
    $('.view').html(html);
  });
  var idx=1;
  element.on('tab(aution_detail)', function(data){
      var num = data.index;
      setTimeout(()=>{
          console.log(55)
          console.log(idx)
          console.log(statenum)
        if(statenum == 99){
            statenum=2
                    }
        if(statenum == 23){
            statenum=21
                    }
        if(statenum == 23){
            statenum=11
                 }
        if(idx<statenum){    
            $('.next').css('display','inline-block');
            }else{
            $('.next').css('display','none');
            }
        for(var i =0;i<set.length;i++){
            $('.setname').eq(i+set.length*num).val(set[i].setname)
            $('.setuse').eq(i+set.length*num).val(set[i].setuse)                      
        }
      },100);
      idx =  this.getAttribute('lay-id');
      if(idx==11||idx==21){
          $('.carshow').css('display','block')
      }else{
          $('.carshow').css('display','none')
      }
      if(idx ==1 ){
        $('.prev').css('display','none')
      }else if(idx ==22){
        $('.next').css('display','none')
      }else{
        $('.next').css('display','inline-block')
        $('.prev').css('display','inline-block')
      }

      
   });
  ajaxauction()
  function ajaxauction(){
  if(id){
   Load();
   var data1={};
   var where_json ={};
   where_json.id=['in','{$Think.get.id}']
   data1.where_json=JSON.stringify(where_json)
   ajax(api.compile.list,data1,'post').done((res)=> {
       Load(1)
       console.log(res)
       if(res.code==1){
           statenum=res.data[0].state;
          idx=res.data[0].state==99?2:res.data[0].state;
          if(res.data[0].state==99){
              $('.approve').css('display','inline-block')        
          }   
    if(res.data[0].strength_type==1||res.data[0].strength_type==2){
        $('.finance').css('display','block');
    }else{
        $('.administration').css('display','block');
    }    
        //页面进入选择的tip
          choosetable(idx)
          
          if(res.data[0].state>1){
              if(res.data[0].state==2||res.data[0].state>3){
                //   $('.state2').remove();  
                  $('.bureau_of_finance_result1').html('<div style="padding:20px">审批已通过</div>')
                  $('.provincial_administration_result1').html('<div style="padding:20px">审批已通过</div>')
              }else if(res.data[0].state==3){
                // $('.state2').remove();
                  $('.bureau_of_finance_result1').html('<div style="padding:20px">审批已拒绝</div>')
                  $('.provincial_administration_result1').html('<div style="padding:20px">审批已拒绝</div>')
              }
              if(res.data[0].state==12||res.data[0].state==21||res.data[0].state==22||res.data[0].state==23){
                // $('.state12').remove(); 
                  $('.bureau_of_resources_result').html('<div style="padding:20px">审批已同意</div>')
              }else if(res.data[0].state==13){
                $('.state12').remove(); 
                  $('.bureau_of_resources_result').html('<div style="padding:20px">审批已拒绝</div>')
              }

              if(res.data[0].state==22){
                // $('.state22').remove(); 
                  $('.bureau_of_finance_result2').html('<div style="padding:20px">审批已同意</div>')
                  $('.provincial_administration_result2').html('<div style="padding:20px">审批已同意</div>')
              }else if(res.data[0].state==23){
                // $('.state22').remove(); 
                  $('.bureau_of_finance_result2').html('<div style="padding:20px">审批已拒绝</div>')
                  $('.provincial_administration_result2').html('<div style="padding:20px">审批已拒绝</div>')
              }
          }
 
      }else{
          Err(res.msg)
      }
       if(res.data[0].state==2){
        $('.bind').css('display','inline-block')
        // 车辆选择
        $('.number').on('click', () => {
        // opsearch(`{:url("html/car/car")}?op=search&company_id=${res.data[0]['company'].company_id}&company_strength_id=0`)
        opsearch(`{:url("html/car/car")}?op=search`)
    })
      }else if(res.data[0].state == 22){
          $('.car').css('display','inline-block')
        $('.number').on('click', () => {
            // opsearch(`{:url("html/car/car")}?op=search&company_id=${res.data[0]['company'].company_id}&company_strength_id=0`)
            opsearch(`{:url("html/car/car")}?op=search`)
    })
}
console.log($('.li'))
      if(res.data[0].state==1){
    //财政局同意新增编制 
      $('.finance_ok').on('click',function(){
        var reason =$('#financereason').val()
          clickauction({
            msg:'是否同意新增编制',
            msgs:'审核通过',
            state:2,
            id:id,
            reason:reason
          })
      })
      $('.finance_no').on('click',function(){      
        var reason =$('#financereason').val()
          clickauction({
            msg:'是否拒绝新增编制',
            msgs:'已驳回审核',
            state:3,
            id:id,
            reason:reason
          })
      })
      //省管局同意新增编制
      $('.administration_ok').on('click',function(){
        var reason =$('#provincialreason').val()
          clickauction({
              msg:'是否同意新增编制',
              msgs:'审核通过',
              state:2,
              id:id,
              reason:reason
          })
      })
      $('.administration_no').on('click',function(){
        var reason =$('#provincialreason').val()
          clickauction({
            msg:'是否拒绝新增编制',
            msgs:'已驳回审核',
            state:3,
            id:id,
            reason:reason
          })
      })
      }else if(res.data[0].state== 2){
        $('.li').eq(1).css('display','inline-block')
        //绑定车辆
      $('.bind').on('click',function(){
        //   console.log($('.car_id').val())
          var car_id =$('.car_id').val()
        //   datas=res.data[0];
        var datas={};
          datas.id=res.data[0].id;
          datas.car_id =car_id;
        //   console.log(datas)
        if(car_id){
            Load();
            var Tip= layer.confirm("是否绑定该车辆", {
              btn: ['确定','取消'] //按钮
            }, function(){
              ajax(api.compile.bind,datas,'post').done(function(res) {
                  Load(1);
                  if(res.code ==1){
                    window.history.go(0);
                      layer.msg('绑定车辆成功', {icon: 1});
                  }else{
                      layer.msg(res.msg, {icon: 2});
                  }
              
              }).fail((err)=>{
                  Load(1);
                  layer.msg('请求失败,稍后再试', {icon: 0});
              })
            }, function(){
              Load(1);
             layer.close(Tip)
            });
        }else{
            Err('请选择车辆')
        }
       
      })
      }else if(res.data[0].state== 99){
        $('.li').eq(1).css('display','inline-block');
         //申请更新编制
      $('.approve').on('click',function(){
        var reason =null;
        clickauction({
            msg:'是否申请新增编制',
            msgs:'已申请,等待审核',
            state:11,
            id:id,
            reason:reason
          })
      })
      }else if(res.data[0].state== 11){
        $('.li').eq(1).css('display','inline-block');
        $('.li').eq(2).css('display','inline-block');
        
       //资产处更新审核
       $('.resources_ok').on('click',function(){ 
        clickauction({
          msg:'是否同意更新编制',
          msgs:'已同意更新编制',
          state:21,
          id:id
        })
    })
    $('.resources_no').on('click',function(){
        clickauction({
          msg:'是否拒绝更新编制',
          msgs:'已拒绝更新编制',
          state:13,
          id:id
        })
    })
      }else if(res.data[0].state== 21){
        $('.li').eq(1).css('display','inline-block');
        $('.li').eq(2).css('display','inline-block');
        $('.li').eq(3).css('display','inline-block');
      //财政局更新审核
      $('.finance_again_ok').on('click',function(){
        var reason =$('#financereason2').val() 
        clickauction({
          msg:'是否同意更新编制',
          msgs:'已同意更新编制',
          state:22,
          id:id,
          reason:reason
        })
    })
    $('.finance_again_no').on('click',function(){
        var reason =$('#financereason2').val()
        clickauction({
          msg:'是否拒绝更新编制',
          msgs:'已拒绝更新编制',
          state:23,
          id:id,
          reason:reason
        })
    })
      //省管局更新审核
      $('.administration_again_ok').on('click',function(){
        var reason =$('#provincialreason2').val()
        clickauction({
            msg:'是否同意更新编制',
            msgs:'已同意更新编制',
            state:22,
            id:id,
            // reason:reason
        })
      })
      $('.administration_again_no').on('click',function(){
        var reason =$('#provincialreason2').val()
          clickauction({
            msg:'是否拒绝更新编制',
            msgs:'拒绝更新编制',
            state:23,
            id:id,
            reason:reason
          })
      })
      }else if(res.data[0].state== 22){
        $('.li').eq(1).css('display','inline-block');
        $('.li').eq(2).css('display','inline-block');
        $('.li').eq(3).css('display','inline-block');
        $('.li').eq(4).css('display','inline-block');
                //更新编制车辆
      //绑定车辆
      $('.updata').on('click',function(){
        // console.log($('.car_id').val())
        var car_id =$('.car_id').val()
      //   datas=res.data[0];
      var datas={};
        datas.id=res.data[0].id;
        datas.car_id =car_id;
        // console.log(datas)
      if(car_id){
          Load();
          var Tip= layer.confirm("是否更新为该车辆", {
            btn: ['确定','取消'] //按钮
          }, function(){
            ajax(api.compile.bind,datas,'post').done(function(res) {
                Load(1);
                if(res.code ==1){
                    window.history.go(0);
                    layer.msg('更新车辆成功', {icon: 1});
                }else{
                    layer.msg(res.msg, {icon: 2});
                }
            
            }).fail((err)=>{
                Load(1);
                layer.msg('请求失败,稍后再试', {icon: 0});
            })
          }, function(){
            Load(1);
           layer.close(Tip)
          });
      }else{
          Err('请选择车辆')
      }
     
    })
      }
      if(res.data[0].state== 23){
        idx=21;
        choosetable(idx)
        $('.li').eq(1).css('display','inline-block');
        $('.li').eq(2).css('display','inline-block');
        $('.li').eq(3).css('display','inline-block');
      }
      if(res.data[0].state== 13){
        idx=11;
        choosetable(idx)
        $('.li').eq(1).css('display','inline-block');
        $('.li').eq(2).css('display','inline-block');
      }





   
      function clickauction(check_data){
          Load();
          var Tip= layer.confirm(check_data.msg, {
            btn: ['确定','取消'] //按钮
          }, function(){
            ajax(api.compile.audit,{id:id,state:check_data.state,reason:check_data.reason},'post').done(function(res) {
                Load(1);
                if(res.code ==1){
                    layer.msg(check_data.msgs, {icon: 1});
                    window.history.go(0);
                }else{
                    layer.msg('请求失败,稍后再试', {icon: 2});
                }
            
            }).fail((err)=>{
                Load(1);
                layer.msg('请求失败,稍后再试', {icon: 0});
            })
          }, function(){
            Load(1);
           layer.close(Tip)
          });
      }
      //上一步
      $('.prev').on('click',function(){
        if(idx ==22){
            idx =21
        }else if(idx==21){
            idx=11
        }else if(idx == 11){
            idx =2
        }else if(idx == 2){
            idx =1
        }
        
          choosetable(idx)
      })
      //下一步
      $('.next').css('display','none');
     
     
      $('.next').on('click',function(){
        if(idx ==1){
            idx =2
        }else if(idx==2){
            idx=11
        }else if(idx == 11){
            idx =21
        }else if(idx == 21){
            idx =22
        }else if(idx ==22){
            $('.next').css('display','none')
        }
          choosetable(idx)
      })
       //切换tab
      function choosetable(idx){
        element.tabChange('aution_detail', idx);
      }
    //获取编制信息
  console.log(res.data[0])
    for(var key in res.data[0]){
        if(key == 'company'){
            $('input[name=company_id]').val(res.data[0]['company'].company_id)
            $('input[name=company_name]').val(res.data[0]['company'].company_name)
        }else if(key == 'strength_type'){              
                if(res.data[0][key]==1){
                    $('.strength_type_name').val('执法执勤用车')
                   
                }else if(res.data[0][key]==2){
                    $('.strength_type_name').val('特种专业技术用车')
                   
                }else if(res.data[0][key]==3){
                    $('.strength_type_name').val('机要通信用车')
                  
                }else if(res.data[0][key]==4){
                    $('.strength_type_name').val('应急保障用车')
                 
                }
        }else if(key == 'approval_document'){
            images =JSON.parse(res.data[0][key]);
            if(res.data[0][key]){
                var imgData = JSON.parse(res.data[0][key]);
                
                if(imgData.approval_img){
                    $('.img1').attr('src',domain+imgData.approval_img)
                }else{
                    $('.img1').attr('src','/assets/img/noimg.gif')
                }
                if(imgData.budget_early_img){
                    console.log()
                    $('.img2').attr('src',domain+imgData.budget_early_img)
                }else{
                    $('.img2').attr('src','/assets/img/noimg.gif')
                }
                if(imgData.budget_add_img){
                    $('.img3').attr('src',domain+imgData.budget_add_img)
                }else{
                    $('.img3').attr('src','/assets/img/noimg.gif')
                }                
            }else{
                $('.img1').attr('src','/assets/img/noimg.gif')
                $('.img2').attr('src','/assets/img/noimg.gif')
            }

        }else if (key == 'car'){
            // console.log(res.data[0]['car'].number)
            $('input[name=car_id]').val(res.data[0]['car'].car_id)
            $('input[name=number]').val(res.data[0]['car'].number)
        }else if(key =='standard'){
            sets =JSON.parse(res.data[0][key]);
            let standardData= JSON.parse(res.data[0][key]);

            for(keys in standardData){
                if(keys =='set'){
                set =standardData[keys];
                var length =standardData[keys].length;
                console.log(length) 
                for(var i =0;i<length;i++){
                        if(i>0){
                        $('.set').after(setHtml) 
                        }  
                        $('.setname').eq(i).val(standardData[keys][i].setname)
                        $('.setuse').eq(i).val(standardData[keys][i].setuse)                         
                    }
                }else if(keys == 'new_energy'){
                    if(standardData[keys]==1){
                        $(`.${keys}`).attr('checked',true)
                    }else{
                        $(`.${keys}`).attr('checked',false)
                    }

                }else if(keys =='car_type'){
                        if(standardData[keys]==1){
                            $('.car_type_name').val('大型客车')
                           
                        }else if(standardData[keys]==2){
                            $('.car_type_name').val('中型客车')
                           
                        }else if(standardData[keys]==3){
                            $('.car_type_name').val('小型客车')
                          
                        }
                    
                }else{
                    $(`.${keys}`).val(standardData[keys])
                }
            }

            for(keys in standardData){
                if(keys == 'set'){                       
                }else{
                    if(standardData.keys){
                        $(`.${keys}`).val(standardData[keys])
                    }else{
                        $(`.${keys}`).attr('placeholder','未填写')  
                    }
                    
                }
               
            }
            
        }else{
            if(res.data[0][key]){
             $('input[name='+key+']').val(res.data[0][key])
            }else{
                $('input[name='+key+']').attr('placeholder','未填写')                  
            }
        }
     
        
    }
    form.render()
   }).fail(()=>{
       Err('请求失败')
   })
   
 }else{
    Err('请选择编制车辆')
 }
   

 
}